﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace WJP.Domain
{
    /// <summary>
    /// 分页信息
    /// </summary>
    [DataContract]
    [Serializable]
    public class PagingInfo
    {
        #region 属性

        private int _pageNumber;
        private int _pageSize;
        private bool _isNeedCount;
        private int _totalCount;

        /// <summary>
        /// 当前页数，从1开始
        /// </summary>
        [DataMember]
        public int PageNumber
        {
            get
            {
                return _pageNumber;
            }
            set
            {
                if (value < 1)
                    throw new ArgumentOutOfRangeException(nameof(value), "当前页数PageNumber必须是正整数");
                _pageNumber = value;
            }
        }

        /// <summary>
        /// 页面大小
        /// </summary>
        [DataMember]
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                if (value <= 0)
                    throw new ArgumentOutOfRangeException(nameof(value), "每页大小PageSize必须是正数");
                _pageSize = value;
            }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        [DataMember]
        public int TotalCount
        {
            get
            {
                if (_totalCount == -1)
                    return 0;
                return _totalCount;
            }
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException(nameof(value), "总记录数TotalCount不能为负数");
                _totalCount = value;
            }
        }

        /// <summary>
        /// 是否需要检索所有记录的计数
        /// <para>如果这是真的，那意味着DAL应该从数据库中检索计数信息</para>
        /// </summary>
        [DataMember]
        public bool IsNeedCount
        {
            get { return _isNeedCount; }
            set
            {
                _isNeedCount = value;
                if (value)
                {
                    if (_totalCount < 0)
                        return;
                    _totalCount = -1;
                }
                else if (_totalCount < 0)
                    _totalCount = 0;

            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                int num = _totalCount / _pageSize;
                if ((_totalCount % _pageSize) > 0)
                    ++num;
                return num;
            }
        }

        /// <summary>
        /// 前一页是否可用
        /// </summary>
        public bool HasPreviousPage
        {
            get
            {
                return _pageNumber > 1;
            }
        }

        /// <summary>
        /// 后一页是否可用
        /// </summary>
        public bool HasNextPage
        {
            get
            {
                return _pageNumber < PageCount;
            }
        }

        /// <summary>
        /// 是否内存页
        /// </summary>
        protected internal bool MemoryPaging { get; set; }

        /// <summary>
        /// 表明没有分页动作的实例
        /// </summary>
        public static readonly PagingInfo Empty = new EmptyPagingInfo();

        #endregion

        #region 构造函数

        /// <summary>
        /// 默认在第1页，每页10条数据
        /// </summary>
        public PagingInfo()
            : this(1, 10)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        public PagingInfo(int pageNumber, int pageSize)
            : this(pageNumber, pageSize, false)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="isNeedCount"></param>
        public PagingInfo(int pageNumber, int pageSize, bool isNeedCount)
        {
            PageNumber = pageNumber;
            PageSize = pageSize;
            IsNeedCount = isNeedCount;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        public PagingInfo(int pageNumber, int pageSize, int totalCount)
        {
            PageNumber = pageNumber;
            PageSize = pageSize;
            TotalCount = totalCount;
        }

        /// <summary>
        /// 反序列化构造函数
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        protected PagingInfo(SerializationInfo info, StreamingContext context)
        {
        }

        #endregion
    }
}
